<?php

class ProductForm extends BaseForm {

    public function setup() {
      $choices = $this->getCategoriesFormatted();
      $lines = $this->getLinesFormatted();
      $companies = $this->getCompaniesFormatted();

        $this->setWidgets(array(
            'category_id'    => new sfWidgetFormChoice(array('choices' => $choices)),
            'line_id'    => new sfWidgetFormChoice(array('choices' => $lines)),
            'company_id'    => new sfWidgetFormChoice(array('choices' => $companies)),
            'name'        => new sfWidgetFormInputText(),
            'description' => new sfWidgetFormTextarea(),
            'price'       => new sfWidgetFormInputText(),
            'img'         => new sfWidgetFormInputFile(),
        ));

        $tmpFilesPath = realpath(realpath(getcwd() . '/images/product_image'));
        
        $this->setValidators(array(
            'category_id' => new sfValidatorChoice(array('choices' => array_keys($choices), 'required' => true), array('invalid'=>'Invalid category.','required'=>'Please enter required field.')),
            'line_id' => new sfValidatorChoice(array('choices' => array_keys($lines), 'required' => true), array('invalid'=>'Invalid Line.','required'=>'Please enter required field.')),
            'company_id' => new sfValidatorChoice(array('choices' => array_keys($companies), 'required' => true), array('invalid'=>'Invalid Company.','required'=>'Please enter required field.')),
            'name' => new sfValidatorString(array('required' => true), array("required" => "Please type product name.")),
            'description' => new sfValidatorString(array('required' => true), array("required" => "Please type your description.")),
            'price' => new sfValidatorNumber(array('min'=> 0.1, 'required' => true , 'max'=> 99999999.99), array('min'=>'Invalid Amount.', 'invalid'=>'Invalid Amount.','max'=>'Invalid Amount.','required'=>'Please enter product price.')),
            'img' => new sfValidatorFile(array('required' => false, 'path' => $tmpFilesPath, 'mime_types' => 'web_images'), array('required' => 'The field is required', 'mime_types' => 'The file only accepts image format.')),
        ));


        $this->widgetSchema->setNameFormat('product[%s]');
    }

    
        
    private function getCategoriesFormatted() {
        $bank_accounts = Doctrine::getTable("Category")->findAll();
        $result = array('' => '');
        
        foreach ($bank_accounts as $bank_account) {
            $result[$bank_account['id']] = $bank_account['name'];
        }
        
        return $result;
    }
    
  private function getLinesFormatted() {
        $bank_accounts = Doctrine::getTable("Line")->findAll();
        $result = array('' => '');
        
        foreach ($bank_accounts as $bank_account) {
            $result[$bank_account['id']] = $bank_account['name'];
        }
        
        return $result;
    }

    private function getCompaniesFormatted() {
        $bank_accounts = Doctrine::getTable("Company")->findAll();
        $result = array('' => '');
        
        foreach ($bank_accounts as $bank_account) {
            $result[$bank_account['id']] = $bank_account['name'];
        }
        
        return $result;
    }
      
    
}

?>
